package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.elm.dto.UserRegistrationDTO;
import cn.edu.tju.elm.dto.UserRegistrationResponseDTO;
import cn.edu.tju.elm.service.UserRegistrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserRegistrationController
 * Package: cn.edu.tju.elm.controller
 * Description: 用户注册控制器
 *
 * @Author Niu Wenhang
 * @Create 2025/9/21 22:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/auth")
@Tag(name = "用户认证", description = "用户注册和认证相关接口")
public class UserRegistrationController {

    @Autowired
    private UserRegistrationService userRegistrationService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "顾客自主注册，注册后权限为USER。确认密码验证建议在前端处理")
    public HttpResult<UserRegistrationResponseDTO> registerUser(@Valid @RequestBody UserRegistrationDTO registrationDTO) {
        try {
            UserRegistrationResponseDTO response = userRegistrationService.registerUser(registrationDTO);
            return HttpResult.success(response);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "注册失败: " + e.getMessage());
        }
    }

    @GetMapping("/check-username")
    @Operation(summary = "检查用户名是否可用", description = "注册前检查用户名是否已被使用")
    public HttpResult<Boolean> checkUsernameAvailability(@RequestParam String username) {
        try {
            boolean available = userRegistrationService.isUsernameAvailable(username);
            return HttpResult.success(available);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "检查用户名失败: " + e.getMessage());
        }
    }

    @GetMapping("/check-email")
    @Operation(summary = "检查邮箱是否可用", description = "注册前检查邮箱是否已被使用")
    public HttpResult<Boolean> checkEmailAvailability(@RequestParam String email) {
        try {
            boolean available = userRegistrationService.isEmailAvailable(email);
            return HttpResult.success(available);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "检查邮箱失败: " + e.getMessage());
        }
    }

    @GetMapping("/check-phone")
    @Operation(summary = "检查手机号是否可用", description = "注册前检查手机号是否已被使用")
    public HttpResult<Boolean> checkPhoneAvailability(@RequestParam String phone) {
        try {
            boolean available = userRegistrationService.isPhoneAvailable(phone);
            return HttpResult.success(available);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "检查手机号失败: " + e.getMessage());
        }
    }
}
